﻿Imports Microsoft.Reporting.WinForms
Imports System.IO

Public Class frmCertificados

    Private Sub frmCertificados_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ReportViewer1.RefreshReport()
    End Sub
    Sub save_pdfCertificado()
        Dim lista As New List(Of frmJson.objRoot)()
        Dim lista2 As New List(Of frmJson.objRoot)()
        Dim evaluacion As New clssEvaluaciones
        Dim dstRecord As New DataSet
        Dim dstRecordOtp As New DataSet
        Dim strId As String = 2
        Dim Rootobject() As clssEvaluaciones.sessionR

        dstRecord.Tables.Add("resultado")
        mdlCRUD.loadRecord(dstRecord, "resultado", "resultado", "certificaciones_id = '" & strId & "' AND prueba_id='3'", Nothing)

        Dim arrRow As Array
        arrRow = dstRecord.Tables("resultado").Rows(0).ItemArray.ToArray

        Rootobject = evaluacion.getEvaluacion(arrRow(3).ToString)
        For i = Rootobject.Count - 1 To 0 Step -1
            If (Rootobject(i).ojb IsNot Nothing) Then
                Dim pregrunta() As Pregunta = Rootobject(i).ojb
                For j = 0 To pregrunta.Count - 1
                    pregrunta(j).id.ToString()
                    Dim dstRecord2 As New DataSet
                    dstRecord2.Tables.Add("pregunta")
                    mdlCRUD.loadRecord(dstRecord2, "pregunta", "pregunta", "pregunta_id = '" & pregrunta(j).id & "'", "")
                    If (dstRecord2.Tables("pregunta").Rows.Count > 0) Then
                        arrRow = dstRecord2.Tables("pregunta").Rows(0).ItemArray.ToArray
                        Dim item As New frmJson.objRoot
                        If (j = 0) Then
                            item.idOLP = Rootobject(i).id.ToString
                        End If
                        item.id = arrRow(1)
                        item.respuesta = pregrunta(j).respuesta
                        item.respuestaOLP = "sasasa"
                        lista.Add(item)
                    End If
                Next j
            End If
        Next i
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("Medicogeneral", lista))

        dstRecordOtp.Tables.Add("resultado")
        mdlCRUD.loadRecord(dstRecordOtp, "resultado", "resultado", "certificaciones_id = '" & strId & "' AND prueba_id='2'", Nothing)

        arrRow = dstRecordOtp.Tables("resultado").Rows(0).ItemArray.ToArray
        Rootobject = evaluacion.getEvaluacion(arrRow(3).ToString)
        For i = Rootobject.Count - 1 To 0 Step -1
            If (Rootobject(i).ojb IsNot Nothing) Then
                Dim pregrunta() As Pregunta = Rootobject(i).ojb
                For j = 0 To pregrunta.Count - 1
                    pregrunta(j).id.ToString()
                    Dim dstRecord2 As New DataSet
                    dstRecord2.Tables.Add("pregunta")
                    mdlCRUD.loadRecord(dstRecord2, "pregunta", "pregunta", "pregunta_id = '" & pregrunta(j).id & "'", "")
                    If (dstRecord2.Tables("pregunta").Rows.Count > 0) Then
                        arrRow = dstRecord2.Tables("pregunta").Rows(0).ItemArray.ToArray
                        Dim item As New frmJson.objRoot
                        If (j = 0) Then
                            item.idOLP = Rootobject(i).id.ToString
                        End If
                        item.id = arrRow(1)
                        item.respuesta = pregrunta(j).respuesta
                        item.respuestaOLP = "sasasa"
                        lista2.Add(item)
                    End If
                Next j
            End If
        Next i
        ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("Optometria", lista2))

        Dim byteViewer As Byte() = ReportViewer1.LocalReport.Render("PDF")
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "*PDF files (*.pdf)|*.pdf"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
        Dim newFile As New FileStream("C:\Users\jortiz\Documents\poryecto src\Certificado.pdf", FileMode.Create)
        newFile.Write(byteViewer, 0, byteViewer.Length)
        newFile.Close()
        Me.ReportViewer1.RefreshReport()
    End Sub
End Class